package top.likeqc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.likeqc.common.lang.Result;
import top.likeqc.entity.Point;
import top.likeqc.service.IPointService;

import java.util.List;

/**
 * <p>
 * 指标点 前端控制器
 * </p>
 *
 * @author likeqc
 * @since 2022-04-11
 */
@RestController
@RequestMapping("/point")
public class PointController extends BaseController {
    @Autowired
    IPointService pointService;

    @GetMapping()
    public Result list(String keyStr) {
        Page<Point> page = pointService.page(getPage(), new QueryWrapper<Point>().like(StringUtils.isNotBlank(keyStr),
            "name",
            keyStr).or().like(StringUtils.isNotBlank(keyStr),
            "remark",
            keyStr));

        return Result.success(page);
    }

    @PostMapping()
    public Result edit(@RequestBody Point point) {
        boolean b = pointService.saveOrUpdate(point);

        if (b) {
            return Result.success(point);
        } else {
            return Result.fail("新增、修改指标点失败");
        }
    }

    @DeleteMapping()
    public Result delete(@RequestBody List<Integer> ids) {

        boolean b = pointService.removeByIds(ids);
        if (b) {
            return Result.success();
        } else {
            return Result.fail("删除指标点失败");
        }
    }


}
